package com.yx.gifts.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yx.gifts.R;
import com.yx.gifts.bean.Gift;

import java.util.List;

/**
 * 横向礼物列表
 */
public class GiftRecyclerAdapter extends RecyclerView.Adapter {
    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    //列表
    private Context mContext;
    private List<Gift> mList;
    private LayoutInflater mLayoutInflater;
    private int clickIndex = -1;//本次点击了

    public GiftRecyclerAdapter(Context context, List< Gift> mLists) {
        this.mContext = context;
        this.mList = mLists;
        this.clickIndex = -1;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void selectItem(int positions) {
        this.clickIndex = positions;
        notifyDataSetChanged();
    }
    public List< Gift> getDataList() {
        return this.mList;
    }
    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }


    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_grid_gift, parent, false);
        return new PersonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PersonViewHolder holders = (PersonViewHolder) holder;
        //holders.nameTv.setText(article);
        Glide.with(mContext)
                .load(mList.get(position).getImage())
                .into(holders.gift_iv);
        holders.itemPrice.setText(mList.get(position).getGold() != null ? mList.get(position).getGold() : "- -");
        //holders.gift_iv.setOnClickListener((v) -> {
        //    clickIndex = position;
        //    notifyDataSetChanged();
        //});

        if (clickIndex == position) {
            //选中了
            holders.item_gg_rl.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        } else {
            //未选中
            holders.item_gg_rl.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        }


        if (onRecyclerViewListener != null) {
            itemOnClick(holder);//让得到整个item.view的点击事件
            //itemOnLongClick(holder);
        }
    }

    public final class PersonViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        private RelativeLayout item_gg_rl;
        private ImageView gift_iv;
        private TextView itemPrice;
        public int position;

        public PersonViewHolder(View itemView) {
            super(itemView);
            gift_iv = (ImageView) itemView.findViewById(R.id.gift_iv_liwu);
            item_gg_rl = (RelativeLayout) itemView.findViewById(R.id.item_gg_rl);
            itemPrice = (TextView) itemView.findViewById(R.id.gift_tv_itemPrice);
        }
    }

    //点击事件//传入当前Holder
    private void itemOnClick(final RecyclerView.ViewHolder holder) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                onRecyclerViewListener.onItemClick(pos);
            }
        });
    }

    //长按事件
    private void itemOnLongClick(final RecyclerView.ViewHolder holder) {
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                onRecyclerViewListener.onItemLongClick(pos);

                //返回true是为了防止触发onClick事件
                return true;
            }
        });
    }

}